package com.ljt.kotlinandmvvm.main.activity

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.ljt.base.bean.RandomCommentDataBean
import com.ljt.base.bean.RandomMusicDataBean
import com.ljt.base.bean.Repo
import com.ljt.base.http.HttpAction
import com.ljt.base.http.HttpUrl.Companion.commentFormat
import com.ljt.base.http.HttpUrl.Companion.musicFormat
import com.ljt.base.http.HttpUrl.Companion.musicSort
import com.ljt.base.bean.ResponseResult
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * @auther :lujiantao
 * @date :2024/4/24 9:23 AM
 * @desc :
 */
public class MainViewModel : ViewModel() {

    var mLiveData:MutableLiveData<String> = MutableLiveData()
    var randomMusicLiveData:MutableLiveData<RandomMusicDataBean> = MutableLiveData()
    var randomCommentLiveData:MutableLiveData<RandomCommentDataBean> = MutableLiveData()
    var httpAction:HttpAction = HttpAction()


    init {

    }

    /**
     * 给mLiveData赋值
     */
    fun setMLiveData(name: String) {

    }

    /**
     * 热歌榜
     */
    fun findHotMusic(){
        var musicDataBean: Call<RandomMusicDataBean> = httpAction.getRandomMusicByCoroutine(
            musicSort, musicFormat)
        musicDataBean.enqueue(object: Callback<RandomMusicDataBean>{
            override fun onResponse(
                call: Call<RandomMusicDataBean>,
                response: Response<RandomMusicDataBean>
            ) {
                Log.e("TAG","获取结果->${Gson().toJson(response.body()!!)}")
                randomMusicLiveData.postValue(response.body())
            }

            override fun onFailure(call: Call<RandomMusicDataBean>, t: Throwable) {
                Log.e("TAG","联网失败")
            }

        })
    }

    /**
     * 获取部分文本描述
     */
    fun findText(){
        var commentDataBean:Call<RandomCommentDataBean> = httpAction.getRandomCommentByCoroutine(commentFormat)
        commentDataBean.enqueue(object:Callback<RandomCommentDataBean>{
            override fun onResponse(
                call: Call<RandomCommentDataBean>,
                response: Response<RandomCommentDataBean>
            ) {
                Log.e("TAG","获取结果->${Gson().toJson(response.body()!!)}")
                randomCommentLiveData.postValue(response.body()!!)
            }

            override fun onFailure(
                call: Call<RandomCommentDataBean>,
                t: Throwable
            ) {

            }

        })
    }

    /**
     * retrofit官网获取name
     */
    fun findUserList(){
        var repos: Call<List<Repo>> = httpAction.findUserList("octocat")
        repos.enqueue(object: Callback<List<Repo>> {
            override fun onResponse(call: Call<List<Repo>>, response: Response<List<Repo>>) {
                Log.e("TAG","请求成功-->"+ response.body()!![0].name)
                mLiveData.postValue(response.body()!![0].name)

            }

            override fun onFailure(call: Call<List<Repo>>, t: Throwable) {
                Log.e("TAG","请求失败-->"+ t.message)
            }
        })
    }

}